package com.ymdx.util.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {

    /**
     * 获取该日期的这天开始
     * @return
     */
    public static Date getDayStart(Date srcdate) {
        Calendar c=Calendar.getInstance();
        c.setTime(srcdate);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取该日期的这天偏离pDay天开始
     * @param srcdate
     * @param pDay 0代表今天，-1代表昨天，1代表明天
     * @return
     */
    public static Date getDayStart(Date srcdate,int pDay) {
        Calendar c=Calendar.getInstance();
        c.setTime(srcdate);
        c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR)+pDay);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取该日期的这天结束(不包含),即该日期下一天开始
     * @return
     */
    public static Date getDayEnd(Date srcdate) {
        Calendar c=Calendar.getInstance();
        c.setTime(srcdate);
        c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR)+1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取该日期的这月开始
     * @return
     */
    public static Date getMonthStart(Date srcdate) {
        Calendar c=Calendar.getInstance();
        c.setTime(srcdate);
        c.set(Calendar.DAY_OF_MONTH ,1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取该日期的这月结束(不包含),即该日期下月开始
     * @return
     */
    public static Date getMonthEnd(Date srcdate) {
        Calendar c=Calendar.getInstance();
        c.setTime(srcdate);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH)+1);
        c.set(Calendar.DAY_OF_MONTH ,1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }


    /**
     * 当天凌晨
     * @param date
     * @return
     * @throws ParseException
     */
    public static int DateToint(Date date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone timeZone=TimeZone.getTimeZone("GMT+8:00");//设置时区
        format.setTimeZone(timeZone);//设置时区
        String str = format.format(date);
        Date dt = format.parse(str);
        long nowtTme = dt.getTime();
        int now = (int) (nowtTme / 1000L);//当天0点时间
        return now;
    }
    /**
     *次日凌晨
     * @param date
     * @return
     * @throws ParseException
     */
    public static int DateTointTomorrow(Date date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone timeZone=TimeZone.getTimeZone("GMT+8:00");//设置时区
        format.setTimeZone(timeZone);//设置时区
        String str = format.format(date);
        Date dt = format.parse(str);
        long nowtTme = dt.getTime();
        int now = (int) (nowtTme / 1000L)+86400;//次日0点时间
        return now;
    }

    /**
     * 求两日期相差天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDistanceDays(Date startDate,Date endDate){
        long time1=startDate.getTime();
        long time2=endDate.getTime();
        long distance=time2-time1;
        return (int)(distance/(1000*60*60*24));
    }

    /**
     * 将当前日期向前推移几个月
     * @param date
     * @param move -1：代表上个月，1 ：代表下个月
     * @return
     * @author yangliwei
     */
    public static Date getMonthMove(Date date,int move){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,move);
        return calendar.getTime();
    }

    /**
     * 移动几分钟
     * @param date
     * @param move
     * @return
     */
    public static Date getMinuteMove(Date date ,Integer move){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE,move);
        return calendar.getTime();
    }

    /**
     * 获取时间字符串
     * 用来保存文件
     * 20170815163951349
     * 2017-08-15 16:39:51  349
     * @param date
     * @return
     */
    public static String getDateString(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(date);
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(getDateString(new Date()));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=sdf.parse("2016-07-14 15:22:33");
        System.out.println("当天开始时间："+sdf.format(getDayStart(date)));
        System.out.println("当天结束时间："+sdf.format(getDayEnd(date)));
        System.out.println("当月开始时间："+sdf.format(getMonthStart(date)));
        System.out.println("当月结束时间："+sdf.format(getMonthEnd(date)));

        System.out.println("当前时间向前推移几个月："+sdf.format(getMonthMove(new Date(),-1)));

        System.out.println(sdf.format(getMinuteMove(new Date(),-3)));
    }
}
